package Core
{
	import DramScript.ExecuteCommandResult;
	
	public class Uzer implements IScriptable, ISerializable {  
		import flash.net.Responder;
		import Core.*;
		import flash.display.MovieClip; sdfsd
		import flash.events.TimerEvent;
	    import flash.utils.Timer;
		import utils.Trace;

	// Data stored in the data base - table "uzers"
	// All variables
//		public var gateway : RemotingConnection;

		// Uzer account Data ---------------------------------------------
		private var DB_uzer_id:Number; // Sequential as Gib accounts are created. Low numbers = status
		private var DB_uzer_name:String; // Shadow this in the AllVars uzername
		private var DB_pwd:String; // Make passwords secure
		private var DB_auth_role:String; // Authoring level of Uzer - "admin","uzer". May not need because of amfphp method validation
		private var DB_email:String; // varchar(100)
		private var DB_url:String; // Link to external website.
		private var DB_account_type:String; // free, premium, creator, volunteer, staff
		private var DB_last_signin_date:String; // Need to use amfphp date conversion code. Or Signout??? How determine auto-signout
		private var DB_account_creation_date:String; // amfphp dates
		// private var DB_god_id:Number; // Which GOD is Uzer a Uzer of? 
		private var DB_vanity_text:String; // Uzer auto-PR
		// Character vars
		private var DB_uzerGOD:String = "FSM"; // XXX for now
		// Runtime variables
		// Will add these to the AllVars ------------------------------------------------
		private var DB_god_wrath:int; // 0-100 (101 values) 
									// 100= GOD hates you utterly 0 = GOD fancies you
		private var DB_current_place:String; //Name of current DramPlace. varchar (256)
		private var DB_gibloons:Number; // int
		// Quest data - 1 quest at a time for now XXX
		private var DB_isOnQuest:Boolean; //
		private var DB_questName:String; // "Open Cave"
		
		private var DB_DramVars: AllVars; // Object containing varnumber, varstring, varboolean objects
		private var _game:Game;

		public function Uzer(dg:Core.Gamer_id:Number, uzerGOD:String)
		{
trace("Uzer Constructor");
			_game = dg;
			DB_uzer_id = uzer_id;
			DB_uzerGOD = uzerGOD;
			// Set up scriptable var's
			initDramVars();
			doReadUzer();
		}
	
			//This function creates the service, calls doRead and waits for results
		private function initDramVars():void
		{
			DB_DramVars = new AllVars();
			DB_DramVars.addVarType("questevent");
			DB_DramVars.addVarType("possession");
			DB_DramVars.addVarType("questsaccomplished");
			// "equipped" varType - possessions that are being uzed "Hat,14" [equipped slot name, possession_id]
			DB_isOnQuest = false;
			DB_questName = "";
		}
		// executeCommand( ----------------------------------------------------
		public function executeCommand(target:String, command:String, paramArr:Array):ExecuteCommandResult {
			var ecr:ExecuteCommandResult = new ExecuteCommandResult( );
			var varType:String = "";
			var branch:Number = 0; // Branch chosen for "if" commands
			if(target != "uzer") {
				Trace.throwError("\nActor.executeCommand target== "+target);
			}
			command = command.toLowerCase();
			
			switch(command){
				case "varstring":
				case "varboolean":
				case "varnumber":
					// Don't check to see that varname is unique
					// Create it or overwrite it
					varType = command; // To make explicit that varnumber command creates varnumber type
					DB_DramVars.setVar(varType,paramArr);
				break;
				case "ifvarnumberless":
				case "ifvarnumbermore":
					branch = DB_DramVars.evaluate(command,paramArr);
					ecr = new ExecuteCommandResult( "BRANCH", branch);
				break;
				case "ifvarnumberequal":
					varType = "varnumber";
				case "ifvarstringequal":
					if(varType == "") { varType = "varstring"; }
				case "ifvarbooleanequal":
					if(varType == "") { varType = "varboolean"; }
				case "ifquestevent":
					if(varType == "") { varType = "questevent"; }
					paramArr[2] = varType;
					branch = DB_DramVars.evaluate("ifvartypeequal",paramArr);
					ecr = new ExecuteCommandResult( "BRANCH", branch);
				break;
				case "ifplace":
	//	trace("Ifplace placeName: "+this.placeName+" paramArr[0]: "+paramArr[0]);
					if ( this.placeName == paramArr[0].toLowerCase() ) {
						branch = 0;
					} else {
						branch = 1;
					}
					ecr = new ExecuteCommandResult( "BRANCH", branch);
			break;
				case "addvarnumber":
					DB_DramVars.incrementVarNumber(paramArr[0],paramArr[1]);
				break;
				case "startquest":
				// XXX Right now allow only one quest.
					if(DB_isOnQuest) {
						ecr = new ExecuteCommandResult( "FAIL");
	trace("ERROR Uzer.executeCommand: startquest: already on quest");
					} else {
						DB_isOnQuest = true;
						DB_questName = paramArr[0];
					}
				break;
				case "finishquest":
					// If not on a quest or quest name is wrong then fail
					if( ( !DB_isOnQuest ) || (DB_questName != paramArr[0] ) ) {
						ecr = new ExecuteCommandResult( "FAIL");
	trace("ERROR Uzer.executeCommand: endquest");
					} else {
						DB_isOnQuest = false;
						DB_questName = "";
						DB_DramVars.destroyVarType("questevent");
						DB_DramVars.setVar("questsaccomplished",new Array(paramArr[0],true));
					}
				break;
				case "setquestevent":
					DB_DramVars.setVar("questevent",paramArr);
				break;
				case "ifonquest":
					if( DB_isOnQuest ) {
						if(paramArr[0] != null) { // Quest specified
							if(DB_questName != paramArr[0]) { // Doesn't match
								branch = 1;
							}
						}
					} else {
						branch = 1;
					}
					ecr = new ExecuteCommandResult( "BRANCH", branch);
				break;
				default:
	trace("Uzer doesn't handle command: "+command);
				break;
			}
			return ecr;
		} // executeCommand
		
		// tick() -----------------------------------------
		// Do action, driven by setInterval by game 
		public function tickTock(event:TimerEvent):void {
			// Is there a need for a tick on Uzer???
			} 
			
		// Data base functions ------------------------------------------
	
		//Ask the remote server to read some posts
		private function doReadUzer():void
		{
	trace("Uzer doReadUzer");
			//arg1: message start, arg2: message offset
	//		var pc:PendingCall = _game.amfphpService.doReadUzer(DB_uzer_id); // How to read 1 row selected on uzer_id?
	//		pc.responder = new RelayResponder(this, 'handleDoReadUzer', null);
			//Remember the offset
		}
			
		//Get data back from server, show the post
		private function handleDoReadUzer(thisRecordSet:Array):void
		{
		//	var thisRecordSet:RecordSet = RecordSet(re.result);
			DB_uzer_name = thisRecordSet.getItemAt(0).uzer_name; 
			DB_pwd = thisRecordSet.getItemAt(0).pwd; 
			DB_auth_role = thisRecordSet.getItemAt(0).auth_role; 
			DB_email = thisRecordSet.getItemAt(0).email; 
			DB_url = thisRecordSet.getItemAt(0).url; 
			DB_account_type = thisRecordSet.getItemAt(0).account_type; 
			DB_account_creation_date = thisRecordSet.getItemAt(0).account_creation_date ; 
			DB_last_signin_date = thisRecordSet.getItemAt(0).last_signin_date; 
			DB_vanity_text = thisRecordSet.getItemAt(0).vanity_text; 
	trace("vanity text: "+DB_vanity_text);
		}
		
		
			
		public function read(paramObj:Object):String {
			var result_st:String = "success";
	trace("Uzer.read() not implemented.");		
			return result_st;
		}
		public function write(paramObj:Object):String {
			var result_st:String = "success";
	trace("Uzer.write() not implemented.");		
			return result_st;
		}
		public function get name():String {
			return DB_uzer_name;
		}
		public function get isOnQuest():Boolean {
				return DB_isOnQuest;
		}
		
		public function get questName():String {
			return DB_questName;
		}
	
		public function get placeName():String {
			return DB_current_place.toLowerCase();
		}
		
		public function get uzer_id():Number {
			return DB_uzer_id;
		}
		
		public function set placeName(placeName:String):void {
			DB_current_place = placeName.toLowerCase();
		}
		public function get dramVars():AllVars {
			return DB_DramVars;
		}
		
		public function get uzerGOD():String {
			return DB_uzerGOD;
		}
	}
}